package com.share51.novel.controller;

import com.share51.novel.common.ViewConstant;
import com.share51.novel.security.annotation.HasReferer;
import com.share51.novel.security.annotation.SetCommonInfo;
import com.share51.novel.service.PageService;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * 页面控制器
 *
 * @author gb
 */
@Controller
public class PageController {

    @Resource
    private PageService pageService;

    /**
     * 前往首页
     *
     * @return
     */
    @SetCommonInfo(all = true)
    @GetMapping({"/", "/index.html"})
    public String toIndex(Model model) {
        pageService.toIndex(model);
        return ViewConstant.INDEX;
    }

    /**
     * 搜索小说
     *
     * @param keywords 关键字
     * @param page 页码
     * @return
     */
    @SetCommonInfo
    @GetMapping("/search.html")
    public String search(String keywords,Integer page) {

        return "search/list";
    }

    /**
     * 获取某个分类下内容
     * @param categoryId 分类编号
     * @param page 页码
     * @return
     */
    @SetCommonInfo
    @GetMapping({"/category/{categoryId}_{page}.html","/category/{categoryId}.html"})
    public String category(@PathVariable Integer categoryId,
                           @PathVariable @RequestParam(defaultValue = "1") Integer page){

        return "category/list";
    }


    /**
     * 查看某本小说的详情
     * @param id 小说的编号
     * @return
     */
    @SetCommonInfo
    @GetMapping("/detail/{id}.html")
    public String detail(@PathVariable Integer id){
        return ViewConstant.NOVEL_DETAIL;
    }

    /**
     * 获取小说章节列表
     * @param id 小说的编号
     * @param page 页码
     * @return
     */
    @SetCommonInfo
    @HasReferer(allowSpider = true)
    @GetMapping("/list/{id}.html")
    public String list(@PathVariable Integer id,Integer page){
        return "novelList";
    }

    /**
     * 获取小说详情
     * @param novelId 小说编号
     * @param id 章节编号
     * @return
     */
    @SetCommonInfo
    @HasReferer(allowSpider = true)
    @GetMapping("/detail/{novelId}/{id}.html")
    public String detail(@PathVariable Integer novelId,@PathVariable Integer id){
        return "detail";
    }

    /**
     * 下载小说
     * @param novelId
     * @param request
     * @return
     */
    @HasReferer
    @GetMapping("/download/{novelId}.zip")
    public ResponseEntity<FileSystemResource> download(@PathVariable Integer novelId, HttpServletRequest request) throws UnsupportedEncodingException {
        return pageService.download(novelId,request);
    }

    /**
     * 获取站点地图
     */
    @GetMapping("/sitemap.xml")
    public void siteMap(){
    }

    // 猜你喜欢

    // 获取小说收藏数

    // 获取小说点赞数

    // 获取小说留言数

    // 获取小说阅读数
}
